package com.google.code.swingonfire.showcase.component.page;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;

import com.google.code.swingonfire.utility.Colors;

public class JPageSample extends JComponent
{

    private static final int STROKE_WIDTH = 32;

    private static final long serialVersionUID = -5121189759120807164L;

    private final String name;

    public JPageSample(String name)
    {
        super();

        this.name = name;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void paint(Graphics graphics)
    {
        Dimension size = getSize();
        Graphics2D g = (Graphics2D) graphics.create();

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);

        //g.setFont(FontCache.getFont(Showcase.FONT_HEADLINE, true, false, 12));
        g.setFont(getFont());
        g.setStroke(new BasicStroke(STROKE_WIDTH));

        int width = size.width - STROKE_WIDTH;
        int height = size.height - STROKE_WIDTH;

        g.setColor(Color.ORANGE);
        g.drawRect(STROKE_WIDTH / 2, STROKE_WIDTH / 2, width, height);

        int diameter = Math.min(width, height);

        g.setColor(Colors.darker(Color.ORANGE, 0.5));
        g.drawOval(STROKE_WIDTH / 2, STROKE_WIDTH / 2, width, height);
        g.drawOval(((width - diameter) / 2) + (STROKE_WIDTH / 2), ((height - diameter) / 2) + (STROKE_WIDTH / 2),
            diameter, diameter);

        FontMetrics fontMetrics = g.getFontMetrics();
        Rectangle2D bounds = fontMetrics.getStringBounds(name, g);

        double scale = Math.min((size.width / 1.5) / bounds.getWidth(), (size.height / 1.5) / bounds.getHeight());

        g.setColor(Color.WHITE);
        g.translate(size.width / 2, size.height / 2);
        g.scale(scale, scale);
        g.drawString(name, (int) (-bounds.getWidth() / 2), fontMetrics.getDescent());
    }

}
